﻿// MedicalRulesEngine.API/Controllers/BaseController.cs
using Microsoft.AspNetCore.Mvc;
using MedicalRulesEngine.Infrastructure.Repositories;

namespace MedicalRulesEngine.API.Controllers;

/// <summary>
/// 控制器基类
/// 提供通用的API响应格式和错误处理
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
public abstract class BaseController : ControllerBase
{
    /// <summary>
    /// 返回成功响应
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    /// <param name="data">响应数据</param>
    /// <param name="message">响应消息</param>
    /// <returns>API响应</returns>
    protected IActionResult Success<T>(T data, string message = "操作成功")
    {
        return Ok(new ApiResponse<T>
        {
            Success = true,
            Message = message,
            Data = data,
            Timestamp = DateTime.UtcNow
        });
    }

    /// <summary>
    /// 返回成功响应（无数据）
    /// </summary>
    /// <param name="message">响应消息</param>
    /// <returns>API响应</returns>
    protected IActionResult Success(string message = "操作成功")
    {
        return Ok(new ApiResponse
        {
            Success = true,
            Message = message,
            Timestamp = DateTime.UtcNow
        });
    }

    /// <summary>
    /// 返回分页成功响应
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    /// <param name="pagedResult">分页结果</param>
    /// <param name="message">响应消息</param>
    /// <returns>API响应</returns>
    protected IActionResult Success<T>(PagedResult<T> pagedResult, string message = "查询成功")
    {
        return Ok(new PagedApiResponse<T>
        {
            Success = true,
            Message = message,
            Data = pagedResult.Data,
            TotalCount = pagedResult.TotalCount,
            PageIndex = pagedResult.PageIndex,
            PageSize = pagedResult.PageSize,
            TotalPages = pagedResult.TotalPages,
            Timestamp = DateTime.UtcNow
        });
    }

    /// <summary>
    /// 返回错误响应
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <param name="statusCode">HTTP状态码</param>
    /// <returns>API响应</returns>
    protected IActionResult Error(string message, int statusCode = 400)
    {
        return StatusCode(statusCode, new ApiResponse
        {
            Success = false,
            Message = message,
            Timestamp = DateTime.UtcNow
        });
    }

    /// <summary>
    /// 返回验证错误响应
    /// </summary>
    /// <param name="errors">验证错误列表</param>
    /// <returns>API响应</returns>
    protected IActionResult ValidationError(IEnumerable<string> errors)
    {
        return BadRequest(new ValidationApiResponse
        {
            Success = false,
            Message = "验证失败",
            Errors = errors.ToList(),
            Timestamp = DateTime.UtcNow
        });
    }

    /// <summary>
    /// 返回未找到响应
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>API响应</returns>
    protected IActionResult NotFound(string message = "资源未找到")
    {
        return NotFound(new ApiResponse
        {
            Success = false,
            Message = message,
            Timestamp = DateTime.UtcNow
        });
    }
}

/// <summary>
/// API响应基类
/// </summary>
public class ApiResponse
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 响应消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 响应时间戳
    /// </summary>
    public DateTime Timestamp { get; set; }
}

/// <summary>
/// 带数据的API响应
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class ApiResponse<T> : ApiResponse
{
    /// <summary>
    /// 响应数据
    /// </summary>
    public T? Data { get; set; }
}

/// <summary>
/// 分页API响应
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class PagedApiResponse<T> : ApiResponse
{
    /// <summary>
    /// 响应数据
    /// </summary>
    public IEnumerable<T>? Data { get; set; }

    /// <summary>
    /// 总记录数
    /// </summary>
    public int TotalCount { get; set; }

    /// <summary>
    /// 页索引
    /// </summary>
    public int PageIndex { get; set; }

    /// <summary>
    /// 页大小
    /// </summary>
    public int PageSize { get; set; }

    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPages { get; set; }
}

/// <summary>
/// 验证错误API响应
/// </summary>
public class ValidationApiResponse : ApiResponse
{
    /// <summary>
    /// 验证错误列表
    /// </summary>
    public List<string> Errors { get; set; } = new();
}
